//
//  VKUpdatePassController.m
//  V-Learn
//
//  Created by liubo on 2019/1/14.
//  Copyright © 2019 yxk. All rights reserved.
//

#import "VKUpdatePassController.h"

@interface VKUpdatePassController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UITextField *oldPasswordTf;
@property (nonatomic, strong) UITextField *newPasswordTf;
@property (nonatomic, strong) UITextField *nextPasswordTf;
@end

@implementation VKUpdatePassController

-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 70;
        [self.view addSubview:_tableView];
        _tableView.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
    }
    return _tableView;
}

-(UITextField *)oldPasswordTf{
    if (_oldPasswordTf == nil) {
        _oldPasswordTf = [[UITextField alloc]init];
        _oldPasswordTf.backgroundColor = [UIColor whiteColor];
        _oldPasswordTf.placeholder = @"请输入旧密码";
        _oldPasswordTf.secureTextEntry = YES;

    }
    return _oldPasswordTf;
}

-(UITextField *)newPasswordTf{
    if (_newPasswordTf == nil) {
        _newPasswordTf = [[UITextField alloc]init];
        _newPasswordTf.backgroundColor = [UIColor whiteColor];
        _newPasswordTf.placeholder = @"请输入新密码";
        _newPasswordTf.secureTextEntry = YES;

    }
    return _newPasswordTf;
}

-(UITextField *)nextPasswordTf{
    if (_nextPasswordTf == nil) {
        _nextPasswordTf = [[UITextField alloc]init];
        _nextPasswordTf.backgroundColor = [UIColor whiteColor];
        _nextPasswordTf.secureTextEntry = YES;
        _nextPasswordTf.placeholder = @"请再次输入新密码";
    }
    return _nextPasswordTf;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.edgesForExtendedLayout = @[];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.title = @"修改密码";

    [self tableView];
}

-(BOOL)checkPassword:(NSString *)password{
    NSString *pattern = @"^(?![0-9]+$)(?![a-zA-Z]+$)[a-zA-Z0-9]{6,20}";
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES%@",pattern];
    BOOL isMatch = [pred evaluateWithObject:password];
    return isMatch;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], 222)];
        view.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
        
        UIView *oldLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 70)];
        self.oldPasswordTf.leftView = oldLabel;
        self.oldPasswordTf.leftViewMode = UITextFieldViewModeAlways;
        [view addSubview:self.oldPasswordTf];
        [_oldPasswordTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.height.equalTo(@70);
            make.top.mas_equalTo(@10);
        }];
        
        UIView *newLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 70)];
        self.newPasswordTf.leftView = newLabel;
        self.newPasswordTf.leftViewMode = UITextFieldViewModeAlways;
        [view addSubview:self.newPasswordTf];
        [_newPasswordTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.height.equalTo(@70);
            make.top.mas_equalTo(_oldPasswordTf.mas_bottom).offset(1);
        }];
        UIView *nextLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 15, 70)];
        self.nextPasswordTf.leftView = nextLabel;
        self.nextPasswordTf.leftViewMode = UITextFieldViewModeAlways;
        [view addSubview:self.nextPasswordTf];
        [_nextPasswordTf mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(@0);
            make.height.equalTo(@70);
            make.top.mas_equalTo(_newPasswordTf.mas_bottom).offset(1);
        }];
        return view;
    }
    
    return nil;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen cz_screenWidth], [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 222)];
        view.backgroundColor = [UIColor cz_colorWithRed:244 green:244 blue:244];
        UIButton *commitBtn = [[UIButton alloc]init];
        [view addSubview:commitBtn];
        [commitBtn setImage:[UIImage imageNamed:@"vkbtn"] forState:UIControlStateNormal];
        [commitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(view).offset(-130);
            make.left.equalTo(@15);
            make.width.equalTo(@([UIScreen cz_screenWidth] - 30));
            make.height.equalTo(@55);
        }];
        commitBtn.imageView.contentMode = UIViewContentModeScaleToFill;
        [commitBtn addTarget:self action:@selector(commitBtnClick) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *commitLabel = [[UILabel alloc]init];
        [view addSubview:commitLabel];
        commitLabel.textColor = [UIColor whiteColor];
        commitLabel.font = [UIFont systemFontOfSize:16];
        commitLabel.text = @"确定";
        [commitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(commitBtn).offset(15);
            make.centerX.equalTo(commitBtn);
        }];
        return view;
    }
    
    return nil;
}

- (void)commitBtnClick{
    NSString *oldPWString = [_oldPasswordTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *newPWString = [_newPasswordTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    NSString *nextPWString = [_nextPasswordTf.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    if (oldPWString.length == 0) {
        [MBProgressHUD showSuccess:@"请输入旧密码" toView:self.view];
        return;
    }
    if (newPWString.length == 0) {
        [MBProgressHUD showSuccess:@"请输入新密码" toView:self.view];
        return;
    }
    if (nextPWString == 0) {
        [MBProgressHUD showSuccess:@"请再次输入新密码" toView:self.view];
        return;
    }

    if (![newPWString isEqualToString:nextPWString]) {
        [MBProgressHUD showSuccess:@"两次填写的密码不一致" toView:self.view];
        return;
    }
    
    
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    hud.labelText = @"正在加载...";
//    [hud show:YES];
    __weak typeof(self)weakSelf = self;
    NSMutableDictionary *pagramsD = @{@"token":[UserModel getUserModel].token,@"newPassword":newPWString,@"oldPassword":oldPWString}.mutableCopy;
    [WYHXNetWorkService netWorkSerViceRequestWithPostUrl:WKMeUpdateNewPassWord withPostParams:pagramsD withBlock:^(NSDictionary *result, NSError *error) {
//        hud.hidden = YES;
        NSString *msg = result[@"msg"];
        if ([result[@"code"]integerValue] == 200) {
            [MBProgressHUD showSuccess:msg toView:self.view];
            dispatch_time_t delayTime = dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0/*延迟执行时间*/ * NSEC_PER_SEC));
            
            dispatch_after(delayTime, dispatch_get_main_queue(), ^{
                [weakSelf.navigationController popViewControllerAnimated:YES];
            });


        }else{
            [MBProgressHUD showSuccess:result[@"msg"] toView:self.view];
        }
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 222;
    }
    return 0.1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    if (section == 0) {
        return [UIScreen cz_screenHeight] - LBNAVBAR_HEIGHT - 222;
    }
    return 0.1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [UITableViewCell new];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

}


@end
